<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html {
        scroll-behavior: smooth;
      }
      #backTop {
        position: fixed;
        right: 20px;
        bottom: 60px;
      }

      div {
        font-size: 100px;
        text-align: center;
        width: 800px;
        height: 400px;
        line-height: 400px;
        margin: 10px auto;
        background-color: #4a9478;
      }

      .hide {
        display: none;
      }
      #look {
        position: fixed;
        top: 20px;
        left: 30px;
      }
    </style>
  </head>
  <body>
    <div>A</div>
    <div>B</div>
    <div id="area">C</div>
    <div>D</div>
    <div>E</div>
    <button id="backTop" class="hide">回到顶部</button>
    <button id="look">c</button>
  </body>
  <script>
    var backTop = document.getElementById('backTop');
    var look = document.getElementById('look');
    var lookarea = document.getElementById('area');
    backTop.addEventListener('click', function () {
      //   document.documentElement.scrollTop = document.body.scrollTop = 0;
      window.scroll(0, 0);
    });

    document.addEventListener('scroll', function () {
      if (window.scrollY >= 100) {
        backTop.classList.remove('hide');
      } else {
        backTop.classList.add('hide');
      }
    });
    // look.addEventListener('click', function () {
    //   var view = document.documentElement.scrollTop || document.body.scrollTop;
    //   var viewTop = lookarea.getBoundingClientRect().top;
    //   document.documentElement.scrollTop = document.body.scrollTop =
    //     view + viewTop;
    // });

    look.addEventListener('click', function () {
      var view = window.scrollY;
      console.log(view);
      var viewTop = lookarea.getBoundingClientRect().top;
      console.log(viewTop);
      window.scroll(0, view + viewTop);
    });
  </script>
</html>
